/*
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.chimos.property.compiler.passone;

import static org.objectweb.asm.Opcodes.ACC_PRIVATE;
import static org.objectweb.asm.Opcodes.ACC_PROTECTED;
import static org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static org.objectweb.asm.Opcodes.ASM4;

import java.util.List;

import org.objectweb.asm.AnnotationVisitor;

import de.chimos.property.annotation.PropertyHint;
import de.chimos.property.compiler.Config;
import de.chimos.property.compiler.PropertyInfo;

/**
 * @author Niklas Hofmann
 */
public class PassOneFieldAnnotationVisitor extends AnnotationVisitor {
    
    private final String className;
    private final String propertyName;
    
    private final String signature;
    private final String signatureGeneric;
    
    private final int fieldAccessLevel;
    
    private final List<PropertyInfo> properties;
    
    private PropertyHint.AccessLevel readAccessLevel = PropertyHint.AccessLevel.DEFAULT;
    private PropertyHint.AccessLevel writeAccessLevel = PropertyHint.AccessLevel.DEFAULT;
    
    private PropertyHint.Serialization propertySerialization = PropertyHint.Serialization.DEFAULT;
    
    private String humanReadablePropertyName = null;
    
    @SuppressWarnings("unused")
	private final Config config;
    
    public PassOneFieldAnnotationVisitor(String className, String propertyName, String signature, String signatureGeneric, int fieldAccessLevel, List<PropertyInfo> properties, Config config)
    {
        super(ASM4);
        
        this.className = className;
        this.propertyName = propertyName;
        
        this.signature = signature;
        this.signatureGeneric = signatureGeneric;
        
        this.fieldAccessLevel = fieldAccessLevel;
        
        this.properties = properties;
        
        this.config = config;
    }

    @Override
    public void visitEnum(String name, String desc, String value) {
        switch (name) {
            case "read":
                readAccessLevel = PropertyHint.AccessLevel.valueOf(value);
                break;
            case "write":
                writeAccessLevel = PropertyHint.AccessLevel.valueOf(value);
                break;
            case "serialization":
                propertySerialization = PropertyHint.Serialization.valueOf(value);
                break;
        }
    }

    @Override
    public void visit(String name, Object value)
    {
        if(name.equals("humanReadableName"))
        {
            humanReadablePropertyName = (String)value;
            
            if(humanReadablePropertyName != null && humanReadablePropertyName.length() == 0)
            {
                humanReadablePropertyName = null;
            }
        }
    }
    
    @Override
    public void visitEnd() {
        try {
        	System.out.println("Found uncompiled property "+className+"/"+propertyName);
            properties.add(new PropertyInfo(
                    className, propertyName, humanReadablePropertyName, signature, signatureGeneric,
                    getAccessLevel(readAccessLevel), getAccessLevel(writeAccessLevel),
                    getEnablePropertySerialization(),
                    false
                    ));
        } catch (Exception ex) {
            throw new IllegalArgumentException(ex);
        }
    }
    
    protected int getAccessLevel(PropertyHint.AccessLevel al)
    {
        switch(al)
        {
            case DEFAULT:
                return fieldAccessLevel;
            
            case PUBLIC:
                return ACC_PUBLIC;
            
            case PROTECTED:
                return ACC_PROTECTED;
            
            case PRIVATE:
                return ACC_PRIVATE;
        }
        
        throw new IllegalStateException();
    }
    
    protected boolean getEnablePropertySerialization()
    {
        switch(propertySerialization)
        {
            case DEFAULT:
                return getAccessLevel(readAccessLevel)==ACC_PUBLIC&&getAccessLevel(writeAccessLevel)==ACC_PUBLIC;
                
            case ENABLE:
                return true;
                
            case DISABLE:
                return false;
        }
        
        throw new IllegalStateException();
    }
    
}
